package com.yc.fruitmarket.biz.impl;

import java.util.List;
import java.util.Map;

import com.yc.fruitmarket.biz.typeBiz;
import com.yc.fruitmarket.dao.DBHelper;

public class typeBizImpl implements typeBiz {
	private DBHelper db = new DBHelper();
		//添加类别
		@Override
		public int add(String name) {
			String sql = "insert into type values(null, ?, 1)";
			int result = db.doUpdate(sql, name);
			return result;
		}
		
		//获得类别
		@Override
		public List<Map<String, String>> gettype() {
			String sql = "select type.tid, type.tname, count(product.pid) as count , type.status from type LEFT JOIN product on type.tid = product.tid GROUP BY type.tname,type.tid";
			List<Map<String, String>> list = db.find(sql);
			return list;
		}
		
		//修改类别
		@Override
		public int updatetype(int tid, String tname) {
			String sql = "update type set tname = ? where tid = ?";
			return db.doUpdate(sql, tname, tid);
		}
		
		//查询类别
		@Override
		public List<Map<String, String>> getbytype(String tname, String status) {
			String sql = "select type.tid, type.tname, count(product.pid) as count, type.status from type LEFT JOIN product on type.tid = product.tid where 1 = 1";
			if(!"".equals(status) && status != null) {
				if(!"-1".equals(status)) {
					sql += " and type.status = " + status;
				}
			}
			if(tname != null || tname != "") {
				sql += " and tname like '%"+tname+"%'";
			}
			sql += " GROUP BY type.tname,type.tid";
			List<Map<String, String>> list = db.find(sql);
			return list;
		}
		//查询类别
	@Override
	public List<Map<String, String>> findTypes() {
		String sql="SELECT type.tid, type.tname, COUNT(product.pid) AS count from type LEFT JOIN product on type.tid = product.tid and product.status=1 GROUP BY type.tname,type.tid";  
		return db.find(sql);
	}
	
	//上架类别
	@Override
	public int up(String tid) {
		String sql = "update type set status = 1 where tid = ?";
		return db.doUpdate(sql, tid);
	}
	
	//下架类别
	@Override
	public int down(String tid) {
		String sql = "update type set status = 0 where tid = ?";
		return db.doUpdate(sql, tid);
	}
	
}
